package com.shop.loan.model;

import com.shop.loan.handler.RecoverUserHandler;
import com.shop.loan.proxy.UserProxy;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.sub_erupt.*;
import xyz.erupt.annotation.sub_field.*;
import xyz.erupt.annotation.sub_field.sub_edit.*;
import xyz.erupt.jpa.model.BaseModel;
import xyz.erupt.upms.model.EruptUser;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

@Getter
@Setter
@Entity
@Table(name = "s_user",
        uniqueConstraints = {
                @UniqueConstraint(columnNames = "mobile")
        })
@Erupt(
        name = "用户",
        filter = @Filter("User.isDelete = false"),
        orderBy = "id desc",
        power = @Power(add = false, delete = false),
        dataProxy = UserProxy.class,
        rowOperation = {
                @RowOperation(
                        title = "用户详情",
                        icon = "fa fa-user",
                        mode = RowOperation.Mode.SINGLE,
                        tpl = @Tpl(
                                path = "/tpl/userDetailByUser.html",
                                width = "50%",
                                engine = Tpl.Engine.Thymeleaf
                        ),
                        type = RowOperation.Type.TPL
                ),
                @RowOperation(
                        title = "恢复用户",
                        icon = "fa fa-reply",
                        mode = RowOperation.Mode.SINGLE,
                        operationHandler = RecoverUserHandler.class,
                        ifExpr = "item.userStatus == '注销'",
                        ifExprBehavior = RowOperation.IfExprBehavior.HIDE
                ),
        },
        drills = {
                @Drill(code = "user_bank",
                        title = "银行卡列表",
                        link = @Link(linkErupt = UserBank.class,  //关联表
                                joinColumn = "userId")),  //关联表达式
        }
)
@SQLDelete(sql = "update s_user set is_delete = true, delete_time = now() where id = ?")
public class User extends BaseModel {

    @Transient
    @EruptField(
            edit = @Edit(title = "基本信息", type = EditType.DIVIDE)
    )
    private String divide_base;

    @EruptField(
            views = @View(title = "id", width = "60px"),
            edit = @Edit(title = "id", search = @Search(), readonly = @Readonly)
    )
    @Column(name = "id", insertable = false, updatable = false)
    private String userId;

    @EruptField(
            edit = @Edit(title = "昵称")
    )
    private String name;


    @EruptField(
            views = @View(title = "姓名", width = "100px"),
            edit = @Edit(title = "姓名", search = @Search(vague = true))
    )
    private String cardName;

    @EruptField(
            views = @View(title = "身份证号码", width = "150px"),
            edit = @Edit(title = "身份证号码", search = @Search(vague = true))
    )
    private String cardNum;

    @EruptField(
            views = @View(title = "手机号码", width = "150px"),
            edit = @Edit(title = "手机号码", search = @Search(vague = true))
    )
    private String mobile;


    @EruptField(
            views = @View(title = "进行中订单上限", show = false, width = "150px"),
            edit = @Edit(title = "进行中订单上限", search = @Search(vague = true))
    )
    private Integer ingNum;


    private String email;

    @ManyToOne
    @EruptField(
            views = @View(title = "注册渠道", column = "name", width = "100px"),
            edit = @Edit(
                    title = "注册渠道",
                    search = @Search(vague = true),
                    type = EditType.REFERENCE_TREE
            )
    )
    @JoinColumn(name = "channel_id")
    private Channel channel;

    @EruptField(
            views = @View(title = "订单结清数量", width = "100px"),
            edit = @Edit(title = "订单结清数量")
    )
    private Integer orderEndNum;

    @Transient
    @EruptField(
            views = @View(title = "风控备注", type = ViewType.SAFE_TEXT, width = "200px")
    )
    private String authRemark;

    @EruptField(
            views = @View(title = "年龄", width = "60px"),
            edit = @Edit(title = "年龄", search = @Search(vague = true))
    )
    private Integer age;

    @EruptField(
            views = @View(title = "性别", width = "60px"),
            edit = @Edit(
                    title = "性别",
                    type = EditType.CHOICE,
                    search = @Search,
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "男", value = "1"),
                                    @VL(label = "女", value = "2"),
                                    @VL(label = "未知", value = "99"),
                            }
                    )
            )
    )
    private Integer gender;

    @EruptField(
            views = @View(title = "风控状态"),
            edit = @Edit(
                    title = "风控状态",
                    type = EditType.BOOLEAN,
                    search = @Search(),
                    boolType = @BoolType(
                            trueText = "通过",
                            falseText = "命中"
                    )
            )
    )
    private Boolean machineStatus = true;


    @EruptField(
            views = @View(title = "状态", width = "80px"),
            edit = @Edit(
                    title = "状态",
                    type = EditType.CHOICE,
                    search = @Search,
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "拉黑", value = "0"),
                                    @VL(label = "正常", value = "1"),
                                    @VL(label = "注销", value = "2"),
                            }
                    )
            )
    )
    private Integer userStatus;

    @EruptField(
            edit = @Edit(
                    title = "客户意愿",
                    type = EditType.CHOICE,
                    search = @Search(),
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "有复借意向", value = "1"),
                                    @VL(label = "无复借意向", value = "2"),
                                    @VL(label = "无人接听", value = "3"),
                                    @VL(label = "其他", value = "9"),
                            }
                    ))
    )
    private Integer userWill;


    private BigDecimal expectQuota;

    private Integer expectDay;

    @EruptField(
            views = @View(title = "是否下单"),
            edit = @Edit(
                    title = "是否下单",
                    type = EditType.BOOLEAN,
                    search = @Search(),
                    boolType = @BoolType(
                            trueText = "已下单",
                            falseText = "未下单"
                    )
            )
    )
    private Boolean createOrder = false;

    @Transient
    @EruptField(
            edit = @Edit(title = "认证状态", type = EditType.DIVIDE)
    )
    private String divide_real;

    @EruptField(
            views = @View(title = "实名认证状态"),
            edit = @Edit(
                    title = "实名认证状态",
                    type = EditType.BOOLEAN,
                    search = @Search(),
                    boolType = @BoolType(
                            trueText = "已认证",
                            falseText = "未认证"
                    )
            )
    )
    private Boolean identityAuthStatus = false;

    @EruptField(
            views = @View(title = "紧急联系人认证状态"),
            edit = @Edit(
                    title = "紧急联系人认证状态",
                    type = EditType.BOOLEAN,
                    search = @Search(),
                    boolType = @BoolType(
                            trueText = "已认证",
                            falseText = "未认证"
                    )
            )
    )
    private Boolean contactAuthStatus = false;

    @EruptField(
            views = @View(title = "银行卡认证状态"),
            edit = @Edit(
                    title = "银行卡认证状态",
                    type = EditType.BOOLEAN,
                    search = @Search(),
                    boolType = @BoolType(
                            trueText = "已认证",
                            falseText = "未认证"
                    )
            )
    )
    private Boolean bankAuthStatus = false;


    private String cardAddress;

    @EruptField(
            edit = @Edit(title = "身份证正面照片",
                    type = EditType.ATTACHMENT,
                    attachmentType = @AttachmentType(type = AttachmentType.Type.IMAGE))
    )
    private String idCardPicFront;

    @EruptField(
            edit = @Edit(title = "身份证反面照片",
                    type = EditType.ATTACHMENT,
                    attachmentType = @AttachmentType(type = AttachmentType.Type.IMAGE))
    )
    private String idCardPicBack;


    @Transient
    @EruptField(
            edit = @Edit(title = "其他", type = EditType.DIVIDE)
    )
    private String divide_other;


    @ManyToOne
    @EruptField(
            views = @View(title = "专属客服", column = "name", width = "100px"),
            edit = @Edit(
                    title = "专属客服",
                    search = @Search(vague = true),
                    type = EditType.REFERENCE_TREE
            )
    )
    @JoinColumn(name = "service_user_id")
    private EruptUser serviceUser;


    @EruptField(
            views = @View(title = "备注", width = "120px"),
            edit = @Edit(title = "备注", type = EditType.TEXTAREA)
    )
    private String remark;

    private String userCheckRes;


    private String registerIp;

    @EruptField(
            views = @View(title = "注册时间"),
            edit = @Edit(title = "注册时间",
                    search = @Search(vague = true),
                    readonly = @Readonly,
                    dateType = @DateType(type = DateType.Type.DATE_TIME))
    )
    private Date createTime;

    private Boolean isDelete = false;

    private Date deleteTime;


    public User(Long id) {
        setId(id);
    }

    public User() {
    }
}
